package com.promise.jokerdream.ui.settings

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.clickable
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.material3.Icon
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.R
import com.promise.jokerdream.database.entity.AbyssGameConfigEntity
import com.promise.jokerdream.model.abyss.AbyssMapping
import com.promise.jokerdream.ui.components.CheckboxOptionRow
import com.promise.jokerdream.ui.components.SettingCard
import com.promise.jokerdream.ui.components.RoundCheckbox
import com.promise.jokerdream.ui.components.SingleSelectHashtagBottomSheet
import com.promise.jokerdream.ui.components.SelectionRow
import com.promise.jokerdream.ui.theme.AppSpacing
import com.promise.jokerdream.ui.theme.AppFontSizes

/**
 * 深渊设置卡片
 */
@Composable
fun AbyssSettingCard(
    config: AbyssGameConfigEntity,
    onAbyssChange: (Boolean) -> Unit,
    onWishPointsChange: (Boolean) -> Unit,
    onExchangeTimesChange: (Boolean) -> Unit,
    onChallengeChange: (Boolean) -> Unit,
    onReviveChange: (Boolean) -> Unit,
    onAbyssTextChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    onExecuteClick: (() -> Unit)? = null
) {
    SettingCard(
        title = "深渊之潮",
        description = "",
        checked = config.enabledSetting,
        onCheckedChange = onAbyssChange,
        modifier = modifier,
        onExecuteClick = onExecuteClick,
        content = {
            if (config.enabledSetting) {
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    // 许愿点开关
                    CheckboxOptionRow(
                        title = "领取许愿点",
                        checked = config.wishPointsEnabled,
                        onCheckedChange = onWishPointsChange
                    )
                    
                    // 兑换次数开关
                    CheckboxOptionRow(
                        title = "兑换挑战次数",
                        checked = config.exchangeTimesEnabled,
                        onCheckedChange = onExchangeTimesChange
                    )
                    
                    // 挑战开关
                    CheckboxOptionRow(
                        title = "挑战深渊Boss",
                        checked = config.challengeEnabled,
                        onCheckedChange = onChallengeChange
                    )
                    
                    // 复活开关（仅在挑战开启时显示）
                    if (config.challengeEnabled) {
                        CheckboxOptionRow(
                            title = "死亡时自动复活",
                            checked = config.reviveEnabled,
                            onCheckedChange = onReviveChange
                        )
                    }
                    
                    Spacer(modifier = Modifier.height(AppSpacing.componentMedium))
                    
                    // 深渊选择区域
                    AbyssSelectionArea(
                        selectedAbyssText = config.selectedAbyssText,
                        onAbyssTextChange = onAbyssTextChange
                    )
                }
            }
        }
    )
}

/**
 * 深渊选择区域
 */
@Composable
private fun AbyssSelectionArea(
    selectedAbyssText: String,
    onAbyssTextChange: (String) -> Unit
) {
    var showBottomSheet by remember { mutableStateOf(false) }
    val selectedAbyssFormat = selectedAbyssText.ifEmpty { "未选择" }
    val recommendedTags = AbyssMapping.getAllIdNameFormats()

    SelectionRow(
        label = "当前深渊：",
        selectedText = selectedAbyssFormat,
        onClick = { showBottomSheet = true }
    )

    // 半浮层
    if (showBottomSheet) {
        SingleSelectHashtagBottomSheet(
            title = "选择深渊",
            initialText = selectedAbyssFormat,
            placeholder = "支持输入1或1-崎岖斗界",
            recommendedTags = recommendedTags,
            onCancel = { showBottomSheet = false },
            onDismiss = { showBottomSheet = false},
            onComplete = { selectedFormat ->
                onAbyssTextChange(selectedFormat)
                showBottomSheet = false
            }
        )
    }
}